package cc.rengu.redp.bizimpl.csmc.service.impl;

import cc.rengu.redp.bizimpl.csmc.common.CsmcConstant;
import cc.rengu.redp.bizimpl.csmc.domain.CClearPlan;
import cc.rengu.redp.bizimpl.csmc.domain.CClearRule;
import cc.rengu.redp.bizimpl.csmc.domain.CInstructionRule;
import cc.rengu.redp.bizimpl.csmc.dao.CInstructionRuleMapper;
import cc.rengu.redp.bizimpl.csmc.service.ICInstructionRuleService;
import cc.rengu.redp.bizimpl.enums.BizDataAuditStatusEnum;
import cc.rengu.redp.bizimpl.enums.BizDataStatusEnum;
import cc.rengu.redp.bizimpl.enums.BizOprEnum;
import cc.rengu.redp.bizimpl.enums.PageOptFlagEnum;
import cc.rengu.redp.bizimpl.upmp.common.BusinessContainer;
import cc.rengu.redp.bizimpl.upmp.domain.BizTmp;
import cc.rengu.redp.bizimpl.upmp.domain.UpmpPrivilegeCheck;
import cc.rengu.redp.bizimpl.upmp.service.*;
import cc.rengu.redp.bizimpl.upmp.vo.UpmpUserInfoVO;
import cc.rengu.redp.bizimpl.utils.Common;
import cc.rengu.redp.bizimpl.utils.CommonUtils;
import cc.rengu.redp.bizimpl.utils.DateUtils;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.exception.RedpException;
import cc.rengu.redp.common.utils.SortUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Description;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author lizk
 */
@Service
@DS("csmc")
public class CInstructionRuleServiceImpl extends ServiceImpl<CInstructionRuleMapper, CInstructionRule> implements ICInstructionRuleService {

    @Autowired
    private IUpmpSysParamService upmpSysParamService;
    @Autowired
    private IUpmpPrivilegeCheckService authPrivilegeCheckService;
    @Autowired
    private IBizTmpService bizTmpService;
    @Autowired
    private IBizHisService bizHisService;
    @Autowired
    private IUpmpSysSeqService sysSeqService;


    private long oprMenuId = 1;
    private String oprMenuName = "清分清算管理:清算指令规则管理";
    private long chkMenuId = 2;
    private String chkMenuName = "清分清算管理:清算指令规则管理";
    private String webNextUrl = "/admin/csmc/cInstructionRule";

    @Override
    public Map<String, Object> findByPage(QueryRequest queryRequest, CInstructionRule instructionRule) {

        Map<String, Object> result = new HashMap<>();

        //获取当前登录人信息
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);

        try {
            String instId = instructionRule.getInstId();
            if (Common.isBlank(instId)) {
                instId = user.getInstId();
            }
            LambdaQueryWrapper<CInstructionRule> queryWrapper = new LambdaQueryWrapper<>();
            if (!StringUtils.isEmpty(instId)) {
                queryWrapper.eq(CInstructionRule::getInstId, instId);
            }
            if (!StringUtils.isEmpty(instructionRule.getInstructionType())) {
                queryWrapper.like(CInstructionRule::getInstructionType, instructionRule.getInstructionType());
            }

            List<CInstructionRule> list = baseMapper.selectList(queryWrapper);
            list = list.stream().filter(CommonUtils.distinctByKey(b -> b.getInstructionType())).collect(Collectors.toList());

            for (CInstructionRule record : list) {
                record.setId(record.getInstructionType().substring(2));
            }
            result.put("rows", list);
            result.put("total", CollectionUtils.isEmpty(list) ? 0 : list.size());

            return result;
        } catch (Exception e) {
            log.error("获取清分清算指令规则管理信息失败", e);
            result.put("rows", null);
            result.put("total", 0);
            return result;
        }
    }

    @Override
    public void adminCInstructionRule(List<CInstructionRule> instructionRuleList, String flag) throws RedpException {

        if (flag.equals("chk") || "stop".equals(flag) || "use".equals(flag) || flag.equals("upd")) {
            if (Common.isBlank(instructionRuleList.get(0).getInstructionType())) {
                log.error("请求参数(instructionType)异常!");
                throw new RedpException("请求参数(instructionType)异常!");
            }
        }

        if (PageOptFlagEnum.OPT_FLAG_ADD.getType().equals(flag)) {
            for (CInstructionRule cInstructionRule : instructionRuleList) {
                cInstructionRule.setInstructionType(generateCode());
            }
        }

        saveData(instructionRuleList, flag);
    }

    @Override
    public IPage<CInstructionRule> cInstructionRuleDialog(QueryRequest queryRequest, CInstructionRule instructionRule) {
        try {
            String instId = instructionRule.getInstId();
            LambdaQueryWrapper<CInstructionRule> queryWrapper = new LambdaQueryWrapper<>();

            if (!StringUtils.isEmpty(instId)) {
                queryWrapper.eq(CInstructionRule::getInstId, instId);
            }
            if (!StringUtils.isEmpty(instructionRule.getInstructionType())) {
                queryWrapper.likeRight(CInstructionRule::getInstructionType, instructionRule.getInstructionType());
            }
            queryWrapper.orderByAsc(CInstructionRule::getSettleRuleIndex);
            Page<CInstructionRule> page = new Page<>();
            SortUtil.handlePageSort(queryRequest, page, true);
            return this.page(page, queryWrapper);
        } catch (Exception e) {
            log.error("获取指令规则信息失败", e);
            return null;
        }
    }

    private void saveData(List<CInstructionRule> cInstructionRuleList, String flag) throws RedpException {

        String lastUpddt = DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS");

        //获取当前登录人信息
//        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
//        Organization org = organizationService.getOrgByCode(user.getOrgCode(), user.getInstId());
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
        String instId = cInstructionRuleList.get(0).getInstId();
        if (Common.isBlank(instId)) {
            if (Common.isNotBlank(user) && Common.isNotBlank(user.getInstId())) {
                instId = user.getInstId();
            }
        }
        if (Common.isNotBlank(user) && Common.isNotBlank(user.getInstId())
                && !user.isAdmin() && !cInstructionRuleList.get(0).getInstId().equals(user.getInstId())) {
            log.error("请求参数与用户身份不符");
            throw new RedpException("请求参数与用户身份不符");
        }


        if (PageOptFlagEnum.OPT_FLAG_ADD.getType().equals(flag)) {
            UpmpPrivilegeCheck privilegeCheck = authPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(cInstructionRuleList.get(0).getInstId(), "instructionRuleList:add");

            if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                // 保存正式表
                for (int idx = 0; idx < cInstructionRuleList.size(); idx++) {
                    CInstructionRule cinstructionrule = cInstructionRuleList.get(idx);
                    cinstructionrule.setId(null);

                    cinstructionrule.setOprId(user.getId());
                    cinstructionrule.setCreateTime(lastUpddt);
                    cinstructionrule.setOprStatus(BizDataStatusEnum.STATUS_DRAFT.getType());
                    cinstructionrule.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_CHECKING.getType());
                }
                this.mergeData(cInstructionRuleList, cInstructionRuleList.get(0).getInstId());

                // 保存临时表
                List<BizTmp> tmpList = new ArrayList<>();
                for (CInstructionRule cinstructionrule : cInstructionRuleList) {
                    BizTmp tmp = new BizTmp();
                    String bizData = JSON.toJSONString(cinstructionrule);
                    if (bizData.length() > 8000) {
                        throw new RedpException("业务数据过长，请联系管理员！");
                    }
                    String ename = cinstructionrule.getClass().getAnnotation(TableName.class).value().toLowerCase();
                    String cname = cinstructionrule.getClass().getAnnotation(Description.class).value();

                    String bizData1;
                    String bizData2 = "";
                    String bizData3 = "";
                    if (bizData.length() > 4000) {
                        bizData2 = bizData.substring(0, 3999);
                        bizData3 = bizData.substring(4000);
                    } else {
                        bizData2 = bizData;
                    }
                    if (bizData3.length() > 4000) {
                        throw new RedpException("业务数据过长，请联系管理员！");
                    }
                    tmp.setBizData1(cinstructionrule.getInstructionType());
                    tmp.setBizData2(bizData2);
                    tmp.setBizData3(bizData3);
                    tmp.setBizOpr(BizOprEnum.OPR_ADD.getType());
                    tmp.setCreateTime(lastUpddt);
                    tmp.setLastOprId(user.getId());
                    tmp.setLastUpdateTime(lastUpddt);
                    tmp.setOprMenuId(String.valueOf(oprMenuId));
                    tmp.setOprMenuName(oprMenuName);
                    tmp.setOprTableCname(cname);
                    tmp.setOprTableEname(ename);
                    tmp.setManageInstId(user.getInstId());
                    tmp.setInstId(user.getInstId());
                    tmp.setBizStatus(BizDataStatusEnum.STATUS_DRAFT.getType());
                    tmp.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_CHECKING.getType());
                    tmp.setRealId(cinstructionrule.getId());
                    tmp.setWebNextUrl(webNextUrl);
                    tmpList.add(tmp);
                }
                bizTmpService.saveOrUpdateBatch(tmpList);
            } else {
                // 保存正式表
                for (int idx = 0; idx < cInstructionRuleList.size(); idx++) {
                    CInstructionRule cinstructionrule = cInstructionRuleList.get(idx);
                    cinstructionrule.setId(null);

                    cinstructionrule.setOprId(user.getId());
                    cinstructionrule.setCreateTime(lastUpddt);
                    cinstructionrule.setOprStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                    cinstructionrule.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_PASS.getType());
                }
                this.mergeData(cInstructionRuleList, cInstructionRuleList.get(0).getInstId());

                // 保存历史表
                for (CInstructionRule cinstructionrule : cInstructionRuleList) {

                    bizHisService.saveBizHisNew(cinstructionrule, cinstructionrule, user, user.getInstId(), BizOprEnum.OPR_UPDATE.getType(),
                            lastUpddt, String.valueOf(oprMenuId), oprMenuName, cinstructionrule.getId(), null, null, null);
                }
            }
        } else if (PageOptFlagEnum.OPT_FLAG_UPDATE.getType().equals(flag)) {

            UpmpPrivilegeCheck privilegeCheck = authPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(cInstructionRuleList.get(0).getInstId(), "instructionRuleList:edit");

            if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                // 修改正式表状态
                this.upstatus(cInstructionRuleList.get(0).getInstructionType(), BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType(), cInstructionRuleList.get(0).getOprStatus(), user.getId(), lastUpddt);
                // 将修改数据同步到临时表
                List<BizTmp> tmpList = new ArrayList<>();
                for (CInstructionRule cinstructionrule : cInstructionRuleList) {
                    cinstructionrule.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType());

                    BizTmp tmp = new BizTmp();
                    String bizData = JSON.toJSONString(cinstructionrule);
                    if (bizData.length() > 8000) {
                        throw new RedpException("业务数据过长，请联系管理员！");
                    }

                    String ename = cinstructionrule.getClass().getAnnotation(TableName.class).value().toLowerCase();
                    String cname = cinstructionrule.getClass().getAnnotation(Description.class).value();

                    String bizData1;
                    String bizData2 = "";
                    String bizData3 = "";
                    if (bizData.length() > 4000) {
                        bizData2 = bizData.substring(0, 3999);
                        bizData3 = bizData.substring(4000);
                    } else {
                        bizData2 = bizData;
                    }
                    if (bizData3.length() > 4000) {
                        throw new RedpException("业务数据过长，请联系管理员！");
                    }
                    tmp.setBizData1(cinstructionrule.getInstructionType());
                    tmp.setBizData2(bizData2);
                    tmp.setBizData3(bizData3);
                    tmp.setBizOpr(BizOprEnum.OPR_UPDATE.getType());
                    tmp.setCreateTime(lastUpddt);
                    tmp.setLastOprId(user.getId());
                    tmp.setLastUpdateTime(lastUpddt);
                    tmp.setOprMenuId(String.valueOf(oprMenuId));
                    tmp.setOprMenuName(oprMenuName);
                    tmp.setOprTableCname(cname);
                    tmp.setOprTableEname(ename);
                    tmp.setManageInstId(user.getInstId());
                     tmp.setInstId(user.getInstId());
                    tmp.setBizStatus(BizDataStatusEnum.STATUS_DRAFT.getType());
                    tmp.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType());
//                    if (Common.isBlank(cinstructionrule.getId())) {
//                        tmp.setRealId(uuid);
//                    } else {
                    tmp.setRealId(cinstructionrule.getId());
//                    }
                    tmp.setWebNextUrl(webNextUrl);
                    tmpList.add(tmp);
                }
                String ename = cInstructionRuleList.getClass().getAnnotation(TableName.class).value().toLowerCase();

                //新增、修改、删除临时表明细记录
                this.BizTmpData(tmpList, user.getInstId(), ename, cInstructionRuleList.get(0).getInstructionType());
            } else {
                List<CInstructionRule> instructiontype = this.getListByInstructionType(cInstructionRuleList.get(0).getInstructionType(), user.getInstId());
                for (int idx = 0; idx < cInstructionRuleList.size(); idx++) {
                    CInstructionRule cinstructionrule = cInstructionRuleList.get(idx);
                    cinstructionrule.setAuditOprId(user.getId());
                    cinstructionrule.setUpdateTime(lastUpddt);
                    cinstructionrule.setOprStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                    cinstructionrule.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_PASS.getType());
                }
                this.mergeData(cInstructionRuleList, cInstructionRuleList.get(0).getInstId());
                //保存历史数据
                this.BizHisData(cInstructionRuleList, instructiontype, user, user.getInstId(), String.valueOf(oprMenuId), oprMenuName, lastUpddt);
            }
        } else if (PageOptFlagEnum.OPT_FLAG_STOP.getType().equals(flag)
                || PageOptFlagEnum.OPT_FLAG_USE.getType().equals(flag)
                || PageOptFlagEnum.OPT_FLAG_CANCEL.getType().equals(flag)) {
            String ename = cInstructionRuleList.get(0).getClass().getAnnotation(TableName.class).value().toLowerCase();

            List<BizTmp> bizTmpList = getBizTmpListByUniqueKey(user.getInstId(), ename, cInstructionRuleList.get(0).getInstructionType());
//            List cInstructionRuleListNew = getListByInstructionType(cInstructionRuleList.get(0).getInstructionType(), user.getInstId());
            List cInstructionRuleListNew = getListByInstructionType(cInstructionRuleList.get(0).getInstructionType(),instId);


            List entitys = new ArrayList<>();
            List bizOpr = new ArrayList<>();
            List realIds = new ArrayList<>();
            UpmpPrivilegeCheck privilegeCheck = new UpmpPrivilegeCheck();
            String auditStatus = "";
            String status = "";
            String bizData2 = "";
            String bizData3 = "";
            if (PageOptFlagEnum.OPT_FLAG_STOP.getType().equals(flag)) {

                privilegeCheck = authPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(user.getInstId(), "清算指令规则管理:停用");
                if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                    //修改正式表数据状态
                    for (CInstructionRule cinstructionrule : cInstructionRuleList) {
                        cinstructionrule.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_STOP_CHECKING.getType());
                        cinstructionrule.setOprStatus(cInstructionRuleList.get(0).getOprStatus());
                        cinstructionrule.setAuditOprId(user.getId());
                        cinstructionrule.setUpdateTime(lastUpddt);
                        cinstructionrule.setStopReason(cInstructionRuleList.get(0).getStopReason());
                    }
                    this.saveOrUpdateBatch(cInstructionRuleList);

                    //修改临时表数据状态
                    for (BizTmp biztmp : bizTmpList) {
                        String bizdata = "";
                        if (null != biztmp.getBizData2()) {
                            bizdata += biztmp.getBizData2();
                        }
                        if (null != biztmp.getBizData3()) {
                            bizdata += biztmp.getBizData3();
                        }
                        CInstructionRule cInstructionRule = JSON.parseObject(bizdata, CInstructionRule.class);
                        cInstructionRule.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_STOP_CHECKING.getType());
                        cInstructionRule.setStopReason(cInstructionRuleList.get(0).getStopReason());
                        String bizData = JSON.toJSONString(cInstructionRule);
                        if (bizData.length() > 4000) {
                            bizData2 = bizData.substring(0, 3999);
                            bizData3 = bizData.substring(4000);
                        } else {
                            bizData2 = bizData;
                        }
                        if (bizData3.length() > 4000) {
                            throw new RedpException("业务数据过长，请联系管理员！");
                        }
                        biztmp.setBizData1(cInstructionRule.getInstructionType());
                        biztmp.setBizData2(bizData2);
                        biztmp.setBizData3(bizData3);
                        biztmp.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_STOP_CHECKING.getType());
                        biztmp.setBizStatus(BizDataStatusEnum.STATUS_DRAFT.getType());
                        biztmp.setBizOpr(BizOprEnum.OPR_STOP.getType());
                    }
                    bizTmpService.saveOrUpdateBatch(bizTmpList);
                } else {
                    for (CInstructionRule cinstructionrule : cInstructionRuleList) {
                        cinstructionrule.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_STOP_PASS.getType());
                        cinstructionrule.setOprStatus(BizDataStatusEnum.STATUS_STOPED.getType());
                        cinstructionrule.setAuditOprId(user.getId());
                        cinstructionrule.setUpdateTime(lastUpddt);
                        cinstructionrule.setStopReason(cInstructionRuleList.get(0).getStopReason());
                        bizOpr.add(BizOprEnum.OPR_STOP.getType());
                        realIds.add(cinstructionrule.getId());
                        LambdaUpdateWrapper<CInstructionRule> updateWrapper=new LambdaUpdateWrapper();
                        updateWrapper.eq(CInstructionRule::getInstId,cinstructionrule.getInstId());
                        updateWrapper.eq(CInstructionRule::getInstructionType,cinstructionrule.getInstructionType());
                        updateWrapper.eq(CInstructionRule::getSettleRuleId,cinstructionrule.getSettleRuleId());
                        updateWrapper.eq(CInstructionRule::getSettleRuleIndex,cinstructionrule.getSettleRuleIndex());
                        this.baseMapper.update(cinstructionrule,updateWrapper);
                    }
                    //修改正式表数据状态
//                    this.saveOrUpdateBatch(cInstructionRuleList);
                    entitys = cInstructionRuleList;
                    bizHisService.saveBizHisList(entitys, cInstructionRuleListNew, user, user.getInstId(), bizOpr, lastUpddt, String.valueOf(oprMenuId), oprMenuName, realIds, null, null, null);
                }
            } else if (PageOptFlagEnum.OPT_FLAG_USE.getType().equals(flag)) {
                privilegeCheck = authPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(user.getInstId(), "清算指令规则管理:启用");
                if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                    //修改正式表数据状态
                    for (CInstructionRule cinstructionrule : cInstructionRuleList) {
                        cinstructionrule.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_USE_CHECKING.getType());
                        cinstructionrule.setOprStatus(cInstructionRuleList.get(0).getOprStatus());
                        cinstructionrule.setAuditOprId(user.getId());
                        cinstructionrule.setUpdateTime(lastUpddt);
                    }
                    this.saveOrUpdateBatch(cInstructionRuleList);
                    //修改临时表数据状态
                    for (BizTmp biztmp : bizTmpList) {
                        String bizdata = "";
                        if (null != biztmp.getBizData2()) {
                            bizdata += biztmp.getBizData2();
                        }
                        if (null != biztmp.getBizData3()) {
                            bizdata += biztmp.getBizData3();
                        }
                        CInstructionRule cInstructionRule = JSON.parseObject(bizdata, CInstructionRule.class);
                        cInstructionRule.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_USE_CHECKING.getType());
                        String bizData = JSON.toJSONString(cInstructionRule);
                        if (bizData.length() > 4000) {
                            bizData2 = bizData.substring(0, 3999);
                            bizData3 = bizData.substring(4000);
                        } else {
                            bizData2 = bizData;
                        }
                        if (bizData3.length() > 4000) {
                            throw new RedpException("业务数据过长，请联系管理员！");
                        }
                        biztmp.setBizData1(cInstructionRule.getInstructionType());
                        biztmp.setBizData2(bizData2);
                        biztmp.setBizData3(bizData3);
                        biztmp.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_USE_CHECKING.getType());
                        biztmp.setBizStatus(BizDataStatusEnum.STATUS_DRAFT.getType());
                        biztmp.setBizOpr(BizOprEnum.OPR_USE.getType());
                    }
                    bizTmpService.saveOrUpdateBatch(bizTmpList);
                } else {
                    for (CInstructionRule cinstructionrule : cInstructionRuleList) {
                        cinstructionrule.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_USE_PASS.getType());
                        cinstructionrule.setOprStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                        cinstructionrule.setAuditOprId(user.getId());
                        cinstructionrule.setUpdateTime(lastUpddt);
                        bizOpr.add(BizOprEnum.OPR_USE.getType());
                        realIds.add(cinstructionrule.getId());
                    }
                    //修改正式表数据状态
                    this.saveOrUpdateBatch(cInstructionRuleList);
                    entitys = cInstructionRuleList;
                    bizHisService.saveBizHisList(entitys, cInstructionRuleListNew, user, user.getInstId(), bizOpr, lastUpddt, String.valueOf(oprMenuId), oprMenuName, realIds, null, null, null);
                }
            } else if (PageOptFlagEnum.OPT_FLAG_CANCEL.getType().equals(flag)) {
                privilegeCheck = authPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(user.getInstId(), "清算指令规则管理:注销");
                if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                    //修改正式表数据状态
                    for (CInstructionRule cinstructionrule : cInstructionRuleList) {
                        cinstructionrule.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_CHECKING.getType());
                        cinstructionrule.setOprStatus(cInstructionRuleList.get(0).getOprStatus());
                        cinstructionrule.setAuditOprId(user.getId());
                        cinstructionrule.setUpdateTime(lastUpddt);
                        cinstructionrule.setCancelReason(cInstructionRuleList.get(0).getCancelReason());
                    }
                    this.saveOrUpdateBatch(cInstructionRuleList);
                    //修改临时表数据状态
                    for (BizTmp biztmp : bizTmpList) {
                        String bizdata = "";
                        if (null != biztmp.getBizData2()) {
                            bizdata += biztmp.getBizData2();
                        }
                        if (null != biztmp.getBizData3()) {
                            bizdata += biztmp.getBizData3();
                        }
                        CInstructionRule cInstructionRule = JSON.parseObject(bizdata, CInstructionRule.class);
                        cInstructionRule.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_CHECKING.getType());
                        cInstructionRule.setCancelReason(cInstructionRuleList.get(0).getCancelReason());
                        String bizData = JSON.toJSONString(cInstructionRule);
                        if (bizData.length() > 4000) {
                            bizData2 = bizData.substring(0, 3999);
                            bizData3 = bizData.substring(4000);
                        } else {
                            bizData2 = bizData;
                        }
                        if (bizData3.length() > 4000) {
                            throw new RedpException("业务数据过长，请联系管理员！");
                        }
                        biztmp.setBizData1(cInstructionRule.getInstructionType());
                        biztmp.setBizData2(bizData2);
                        biztmp.setBizData3(bizData3);
                        biztmp.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_CHECKING.getType());
                        biztmp.setBizStatus(BizDataStatusEnum.STATUS_DRAFT.getType());
                        biztmp.setBizOpr(BizOprEnum.OPR_CANCEL.getType());
                    }
                    bizTmpService.saveOrUpdateBatch(bizTmpList);
                } else {
                    for (CInstructionRule cinstructionrule : cInstructionRuleList) {
                        cinstructionrule.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_PASS.getType());
                        cinstructionrule.setOprStatus(BizDataStatusEnum.STATUS_CANCELED.getType());
                        cinstructionrule.setAuditOprId(user.getId());
                        cinstructionrule.setUpdateTime(lastUpddt);
                        bizOpr.add(BizOprEnum.OPR_CANCEL.getType());
                        realIds.add(cinstructionrule.getId());
                    }
                    //修改正式表数据状态
                    this.saveOrUpdateBatch(cInstructionRuleList);
                    entitys = cInstructionRuleList;
                    bizHisService.saveBizHisList(entitys, cInstructionRuleListNew, user, user.getInstId(), bizOpr, lastUpddt, String.valueOf(oprMenuId), oprMenuName, realIds, null, null, null);
                }
            }

        } else if (PageOptFlagEnum.OPT_FLAG_CHECK.getType().equals(flag)) {

            List<CInstructionRule> cInstructionRuleListNew = getListByInstructionType(cInstructionRuleList.get(0).getInstructionType(), user.getInstId());
            List<CInstructionRule> cinstructionrulelist = new ArrayList();

            String ename = cInstructionRuleList.getClass().getAnnotation(TableName.class).value().toLowerCase();

            //获取临时表数据并转换成实体对象
            List<BizTmp> bizTmpList = getBizTmpListByUniqueKey(user.getInstId(), ename, cInstructionRuleList.get(0).getInstructionType());
            if (Common.isBlank(bizTmpList)) {
                log.error("根据id没有找到指定临时表数据！");
                throw new RedpException("根据id没有找到指定临时表数据！");
            }
            for (BizTmp biztmp : bizTmpList) {
                String bizdata = "";
                if (null != biztmp.getBizData2()) {
                    bizdata += biztmp.getBizData2();
                }
                if (null != biztmp.getBizData3()) {
                    bizdata += biztmp.getBizData3();
                }
                CInstructionRule cInstructionRule = JSON.parseObject(bizdata, CInstructionRule.class);
                if (Common.isBlank(cInstructionRule.getId())) {
                    cInstructionRule.setId(biztmp.getRealId());
                }
                cinstructionrulelist.add(cInstructionRule);
            }


            String opt = cInstructionRuleList.get(0).getOpt();
            if (Common.isBlank(opt)) {
                log.error("请求参数(opt)异常!");
                throw new RedpException("请求参数(opt)异常!");
            }

            String auditStatus = "";
            String impStatus = "";
            String bizData2 = "";
            String bizData3 = "";
            List entitys = new ArrayList<>();
            List entityOlds = new ArrayList<>();
            List bizOpr = new ArrayList<>();
            List realIds = new ArrayList<>();
            if (BizDataAuditStatusEnum.AUDIT_STATUS_ADD_CHECKING.getType().equals(cInstructionRuleList.get(0).getAuditStatus())) {//新增审核
                if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(opt)) {
                    //修改正式表审核状态为新增审核通过数据状态为正式
                    for (CInstructionRule cInstructionRule : cinstructionrulelist) {
                        cInstructionRule.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_PASS.getType());
                        cInstructionRule.setOprStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                        cInstructionRule.setAuditTime(lastUpddt);
                        bizOpr.add(BizOprEnum.OPR_ADD.getType());
                        realIds.add(cInstructionRule.getId());
                    }
                    this.saveOrUpdateBatch(cinstructionrulelist);
                    //修改临时表审核状态为新增审核通过数据状态为正式
                    for (BizTmp biztmp : bizTmpList) {
                        String bizdata = "";
                        if (null != biztmp.getBizData2()) {
                            bizdata += biztmp.getBizData2();
                        }
                        if (null != biztmp.getBizData3()) {
                            bizdata += biztmp.getBizData3();
                        }
                        CInstructionRule cInstructionRule = JSON.parseObject(bizdata, CInstructionRule.class);
                        cInstructionRule.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_PASS.getType());
                        cInstructionRule.setOprStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                        String bizData = JSON.toJSONString(cInstructionRule);
                        if (bizData.length() > 4000) {
                            bizData2 = bizData.substring(0, 3999);
                            bizData3 = bizData.substring(4000);
                        } else {
                            bizData2 = bizData;
                        }
                        if (bizData3.length() > 4000) {
                            throw new RedpException("业务数据过长，请联系管理员！");
                        }
                        biztmp.setBizData1(cInstructionRule.getInstructionType());
                        biztmp.setBizData2(bizData2);
                        biztmp.setBizData3(bizData3);
                        biztmp.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_PASS.getType());
                        biztmp.setBizStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                    }
                    bizTmpService.saveOrUpdateBatch(bizTmpList);
                    entitys = cinstructionrulelist;
                    entityOlds = cInstructionRuleListNew;
                    //保存数据到历史表
                    bizHisService.saveBizHisList(entitys, entityOlds, user, user.getInstId(), bizOpr, lastUpddt, String.valueOf(oprMenuId), oprMenuName, realIds, null, null, null);

                } else {
                    //修改正式表审核状态为新增审批拒绝数据状态为非正式
                    for (CInstructionRule cInstructionRule : cInstructionRuleListNew) {
                        cInstructionRule.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_REJECT.getType());
                        cInstructionRule.setOprStatus(BizDataStatusEnum.STATUS_DRAFT.getType());
                        cInstructionRule.setAuditRefuseReason(cInstructionRuleList.get(0).getAuditRefuseReason());
                        cInstructionRule.setAuditTime(lastUpddt);
                    }
                    this.saveOrUpdateBatch(cInstructionRuleListNew);
                    //修改临时表审核状态为新增审批拒绝，数据状态为非正式
                    for (BizTmp biztmp : bizTmpList) {
                        biztmp.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_REJECT.getType());
                        biztmp.setBizStatus(BizDataStatusEnum.STATUS_DRAFT.getType());
                        biztmp.setLastOprId(user.getId());
                        biztmp.setLastUpdateTime(lastUpddt);
                    }
                    bizTmpService.saveOrUpdateBatch(bizTmpList);
                }
            } else if (BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_CHECKING.getType().equals(cInstructionRuleList.get(0).getAuditStatus())) {//注销审核
                if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(opt)) {
                    //修改正式表审核状态为注销审核通过数据状态为已注销
                    for (CInstructionRule cInstructionRule : cinstructionrulelist) {
                        cInstructionRule.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_PASS.getType());
                        cInstructionRule.setOprStatus(BizDataStatusEnum.STATUS_CANCELED.getType());
                        cInstructionRule.setAuditTime(lastUpddt);
                        bizOpr.add(BizOprEnum.OPR_CANCEL.getType());
                        realIds.add(cInstructionRule.getId());
                    }
                    this.saveOrUpdateBatch(cinstructionrulelist);
                    //修改临时表审核状态为注销审核通过数据状态为已注销
                    for (BizTmp biztmp : bizTmpList) {
                        String bizdata = "";
                        if (null != biztmp.getBizData2()) {
                            bizdata += biztmp.getBizData2();
                        }
                        if (null != biztmp.getBizData3()) {
                            bizdata += biztmp.getBizData3();
                        }
                        CInstructionRule cInstructionRule = JSON.parseObject(bizdata, CInstructionRule.class);
                        cInstructionRule.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_PASS.getType());
                        cInstructionRule.setOprStatus(BizDataStatusEnum.STATUS_CANCELED.getType());
                        String bizData = JSON.toJSONString(cInstructionRule);
                        if (bizData.length() > 4000) {
                            bizData2 = bizData.substring(0, 3999);
                            bizData3 = bizData.substring(4000);
                        } else {
                            bizData2 = bizData;
                        }
                        if (bizData3.length() > 4000) {
                            throw new RedpException("业务数据过长，请联系管理员！");
                        }
                        biztmp.setBizData1(cInstructionRule.getInstructionType());
                        biztmp.setBizData2(bizData2);
                        biztmp.setBizData3(bizData3);
                        biztmp.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_PASS.getType());
                        biztmp.setBizStatus(BizDataStatusEnum.STATUS_CANCELED.getType());
                    }
                    bizTmpService.saveOrUpdateBatch(bizTmpList);
                    entitys = cinstructionrulelist;
                    entityOlds = cInstructionRuleListNew;
                    //保存数据到历史表
                    bizHisService.saveBizHisList(entitys, entityOlds, user, user.getInstId(), bizOpr, lastUpddt, String.valueOf(oprMenuId), oprMenuName, realIds, null, null, null);
                } else {
                    //修改正式表审核状态为注销审批拒绝
                    for (CInstructionRule cInstructionRule : cInstructionRuleListNew) {
                        cInstructionRule.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_REJECT.getType());
                        cInstructionRule.setAuditRefuseReason(cInstructionRuleList.get(0).getAuditRefuseReason());
                        cInstructionRule.setAuditTime(lastUpddt);
                    }
                    this.saveOrUpdateBatch(cInstructionRuleListNew);
                    //修改临时表审核状态为注销审批拒绝，数据状态为非正式
                    for (BizTmp biztmp : bizTmpList) {
                        biztmp.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_REJECT.getType());
                        biztmp.setBizStatus(BizDataStatusEnum.STATUS_DRAFT.getType());
                        biztmp.setLastOprId(user.getId());
                        biztmp.setLastUpdateTime(lastUpddt);
                    }
                    bizTmpService.saveOrUpdateBatch(bizTmpList);
                }
            } else if (BizDataAuditStatusEnum.AUDIT_STATUS_STOP_CHECKING.getType().equals(cInstructionRuleList.get(0).getAuditStatus())) {//停用审核
                if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(opt)) {
                    //修改正式表审核状态为停用审核通过数据状态为已停用
                    for (CInstructionRule cInstructionRule : cinstructionrulelist) {
                        cInstructionRule.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_STOP_PASS.getType());
                        cInstructionRule.setOprStatus(BizDataStatusEnum.STATUS_STOPED.getType());
                        cInstructionRule.setAuditTime(lastUpddt);
                        bizOpr.add(BizOprEnum.OPR_STOP.getType());
                        realIds.add(cInstructionRule.getId());
                    }
                    this.saveOrUpdateBatch(cinstructionrulelist);
                    //修改临时表审核状态为停用审核通过数据状态为已停用
                    for (BizTmp biztmp : bizTmpList) {
                        String bizdata = "";
                        if (null != biztmp.getBizData2()) {
                            bizdata += biztmp.getBizData2();
                        }
                        if (null != biztmp.getBizData3()) {
                            bizdata += biztmp.getBizData3();
                        }
                        CInstructionRule cInstructionRule = JSON.parseObject(bizdata, CInstructionRule.class);
                        cInstructionRule.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_STOP_PASS.getType());
                        cInstructionRule.setOprStatus(BizDataStatusEnum.STATUS_STOPED.getType());
                        String bizData = JSON.toJSONString(cInstructionRule);
                        if (bizData.length() > 4000) {
                            bizData2 = bizData.substring(0, 3999);
                            bizData3 = bizData.substring(4000);
                        } else {
                            bizData2 = bizData;
                        }
                        if (bizData3.length() > 4000) {
                            throw new RedpException("业务数据过长，请联系管理员！");
                        }
                        biztmp.setBizData1(cInstructionRule.getInstructionType());
                        biztmp.setBizData2(bizData2);
                        biztmp.setBizData3(bizData3);
                        biztmp.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_STOP_PASS.getType());
                        biztmp.setBizStatus(BizDataStatusEnum.STATUS_STOPED.getType());
                    }
                    bizTmpService.saveOrUpdateBatch(bizTmpList);
                    entitys = cinstructionrulelist;
                    entityOlds = cInstructionRuleListNew;
                    //保存数据到历史表
                    bizHisService.saveBizHisList(entitys, entityOlds, user, user.getInstId(), bizOpr, lastUpddt, String.valueOf(oprMenuId), oprMenuName, realIds, null, null, null);
                } else {
                    //修改正式表审核状态为停用审批拒绝
                    for (CInstructionRule cInstructionRule : cInstructionRuleListNew) {
                        cInstructionRule.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_STOP_REJECT.getType());
                        cInstructionRule.setAuditRefuseReason(cInstructionRuleList.get(0).getAuditRefuseReason());
                        cInstructionRule.setAuditTime(lastUpddt);
                    }
                    this.saveOrUpdateBatch(cInstructionRuleListNew);
                    //修改临时表审核状态为停用审批拒绝，数据状态为非正式
                    for (BizTmp biztmp : bizTmpList) {
                        biztmp.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_STOP_REJECT.getType());
                        biztmp.setBizStatus(BizDataStatusEnum.STATUS_DRAFT.getType());
                        biztmp.setLastOprId(user.getId());
                        biztmp.setLastUpdateTime(lastUpddt);
                    }
                    bizTmpService.saveOrUpdateBatch(bizTmpList);
                }
            } else if (BizDataAuditStatusEnum.AUDIT_STATUS_USE_CHECKING.getType().equals(cInstructionRuleList.get(0).getAuditStatus())) {//启用审核
                if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(opt)) {
                    //修改正式表审核状态为启用审核通过数据状态为正式
                    for (CInstructionRule cInstructionRule : cinstructionrulelist) {
                        cInstructionRule.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_USE_PASS.getType());
                        cInstructionRule.setOprStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                        cInstructionRule.setAuditTime(lastUpddt);
                        bizOpr.add(BizOprEnum.OPR_USE.getType());
                        realIds.add(cInstructionRule.getId());
                    }
                    this.saveOrUpdateBatch(cinstructionrulelist);
                    //修改临时表审核状态为启用审核通过数据状态为正式
                    for (BizTmp biztmp : bizTmpList) {
                        String bizdata = "";
                        if (null != biztmp.getBizData2()) {
                            bizdata += biztmp.getBizData2();
                        }
                        if (null != biztmp.getBizData3()) {
                            bizdata += biztmp.getBizData3();
                        }
                        CInstructionRule cInstructionRule = JSON.parseObject(bizdata, CInstructionRule.class);
                        cInstructionRule.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_USE_PASS.getType());
                        cInstructionRule.setOprStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                        String bizData = JSON.toJSONString(cInstructionRule);
                        if (bizData.length() > 4000) {
                            bizData2 = bizData.substring(0, 3999);
                            bizData3 = bizData.substring(4000);
                        } else {
                            bizData2 = bizData;
                        }
                        if (bizData3.length() > 4000) {
                            throw new RedpException("业务数据过长，请联系管理员！");
                        }
                        biztmp.setBizData1(cInstructionRule.getInstructionType());
                        biztmp.setBizData2(bizData2);
                        biztmp.setBizData3(bizData3);
                        biztmp.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_USE_PASS.getType());
                        biztmp.setBizStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                    }
                    bizTmpService.saveOrUpdateBatch(bizTmpList);
                    entitys = cinstructionrulelist;
                    entityOlds = cInstructionRuleListNew;
                    //保存数据到历史表
                    bizHisService.saveBizHisList(entitys, entityOlds, user, user.getInstId(), bizOpr, lastUpddt, String.valueOf(oprMenuId), oprMenuName, realIds, null, null, null);
                } else {
                    //修改正式表审核状态为停用审批拒绝
                    for (CInstructionRule cInstructionRule : cInstructionRuleListNew) {
                        cInstructionRule.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_USE_REJECT.getType());
                        cInstructionRule.setAuditRefuseReason(cInstructionRuleList.get(0).getAuditRefuseReason());
                        cInstructionRule.setAuditTime(lastUpddt);
                    }
                    this.saveOrUpdateBatch(cInstructionRuleListNew);
                    //修改临时表审核状态为启用审批拒绝，数据状态为非正式
                    for (BizTmp biztmp : bizTmpList) {
                        biztmp.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_USE_REJECT.getType());
                        biztmp.setBizStatus(BizDataStatusEnum.STATUS_DRAFT.getType());
                        biztmp.setLastOprId(user.getId());
                        biztmp.setLastUpdateTime(lastUpddt);
                    }
                    bizTmpService.saveOrUpdateBatch(bizTmpList);
                }
            } else if (BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType().equals(cInstructionRuleList.get(0).getAuditStatus())) {//修改审核
                //保存正式表数据
                List<CInstructionRule> instructiontype = getListByInstructionType(cInstructionRuleList.get(0).getInstructionType(), user.getInstId());

                if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(opt)) {
                    for (CInstructionRule cInstructionRule : cinstructionrulelist) {
                        cInstructionRule.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_PASS.getType());
                        cInstructionRule.setOprStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                        cInstructionRule.setAuditTime(lastUpddt);
                        cInstructionRule.setAuditOprId(user.getId());
                        cInstructionRule.setUpdateTime(lastUpddt);
                    }
                    this.mergeData(cinstructionrulelist, cinstructionrulelist.get(0).getInstId());
                    //修改临时表审核状态为新增审核通过，数据状态为正式
                    for (BizTmp biztmp : bizTmpList) {
                        biztmp.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_PASS.getType());
                        biztmp.setBizStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                        biztmp.setLastOprId(user.getId());
                        biztmp.setLastUpdateTime(lastUpddt);
                    }
                    bizTmpService.saveOrUpdateBatch(bizTmpList);
                    //保存历史数据

                    BizHisData(cinstructionrulelist, instructiontype, user, user.getInstId(), String.valueOf(oprMenuId), oprMenuName, lastUpddt);
                } else {
                    //修改临时表的审核状态为修改审核拒绝，数据状态为非正式
                    for (BizTmp biztmp : bizTmpList) {
                        biztmp.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_REJECT.getType());
                        biztmp.setBizStatus(BizDataStatusEnum.STATUS_DRAFT.getType());
                        biztmp.setLastOprId(user.getId());
                        biztmp.setLastUpdateTime(lastUpddt);
                    }
                    //修改正式表的审核状态，
                    upstatus(cInstructionRuleList.get(0).getInstructionType(), BizDataAuditStatusEnum.AUDIT_STATUS_UPD_REJECT.getType(), cInstructionRuleList.get(0).getOprStatus(), user.getId(), lastUpddt);

                }
            }
        }

    }

    private List<BizTmp> getBizTmpListByUniqueKey(String instId, String ename, String instructionType) {
        LambdaQueryWrapper<BizTmp> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BizTmp::getInstId, instId).eq(BizTmp::getOprTableEname, ename).eq(BizTmp::getBizData1, instructionType);
        List<BizTmp> list = bizTmpService.list(wrapper);
        if (Common.isNotBlank(list) && list.size() > 0) {
            return list;
        }
        return null;
    }

    /**
     * 系统生成会计分录标识（格式：AE + 6位序号）
     *
     * @return
     */
    private String generateCode() {
        // 固定入参
        String common = "COMM_PARAM_CODE";
        // 会计分录标识前缀：IN
        String prefix = CsmcConstant.InstructionType_PREFIX;
//        // 渠道ID：固定值20
//        String channelId = CommonUtils.getChannelId(dataDictService);
        // 生成序列号
        String serialNumber = sysSeqService.updateParamValue(common, prefix, "");
        // 编码：AE + 6位顺序编号
        String result = prefix + String.format("%06d", Integer.parseInt(serialNumber));
        return result;
    }

    /**
     * 新增、修改、删除明细记录
     *
     * @param cInstructionRuleList
     * @throws RedpException
     */
    public void mergeData(List<CInstructionRule> cInstructionRuleList, String instId) throws RedpException {

        List<CInstructionRule> listOrg = getListByInstructionType(cInstructionRuleList.get(0).getInstructionType(), instId);
        List<CInstructionRule> listDel = new ArrayList<CInstructionRule>();//待删除的数据
        List<CInstructionRule> listNew = new ArrayList<CInstructionRule>();//待新增的数据

        if (null != listOrg) {
            Iterator<CInstructionRule> itMsir = listOrg.iterator();
            while (itMsir.hasNext()) {
                CInstructionRule msto = (CInstructionRule) itMsir.next();
                int flag = 0;

                Iterator<CInstructionRule> itMsi = cInstructionRuleList.iterator();
                while (itMsi.hasNext()) {
                    CInstructionRule mst = (CInstructionRule) itMsi.next();
                    if (msto.getId().equals(mst.getId())) {
                        flag = 1;
                        break;
                    }
                }
                if (flag == 0) { // 正式表的数据在临时表中已经不存在
                    listDel.add(msto);
                }
            }
        }

        if (null != cInstructionRuleList) {//分析出待新增的数据
            Iterator<CInstructionRule> itMsir = cInstructionRuleList.iterator();
            while (itMsir.hasNext()) {
                CInstructionRule mst = (CInstructionRule) itMsir.next();
                int flag = 0;

                Iterator<CInstructionRule> itMso = listOrg.iterator();
                while (itMso.hasNext()) {
                    CInstructionRule msto = (CInstructionRule) itMso.next();
                    if (msto.getId().equals(mst.getId())) {
                        flag = 1;
                        BeanUtils.copyProperties(mst, msto);
                        break;
                    }
                }
                if (flag == 0) { // 临时表中不存在的数据
                    listNew.add(mst);
                }
            }
        }
        if (Common.isNotBlank(listOrg) && listOrg.size() > 0) {//修改
            for (CInstructionRule clearPlan : listOrg) {
                LambdaUpdateWrapper<CInstructionRule> wrapper = new LambdaUpdateWrapper<>();
                wrapper.eq(CInstructionRule::getInstructionType, clearPlan.getInstructionType()).eq(CInstructionRule::getSettleRuleIndex, clearPlan.getSettleRuleIndex());
                baseMapper.update(clearPlan, wrapper);
            }
        }

        if (Common.isNotBlank(listNew) && listNew.size() > 0) {//新增
            for (CInstructionRule rule : listNew) {
                rule.setCreateTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
                this.save(rule);
            }

        }
        if (Common.isNotBlank(listDel) && listDel.size() > 0) {//删除
            for (CInstructionRule clearPlan : listDel) {
                LambdaUpdateWrapper<CInstructionRule> wrapper = new LambdaUpdateWrapper<>();
                wrapper.eq(CInstructionRule::getInstructionType, clearPlan.getInstructionType()).eq(CInstructionRule::getSettleRuleIndex, clearPlan.getSettleRuleIndex());
                baseMapper.delete(wrapper);
            }
        }
    }

    private List<CInstructionRule> getListByInstructionType(String instructionType, String instId) {
        LambdaQueryWrapper<CInstructionRule> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(CInstructionRule::getInstructionType, instructionType).eq(CInstructionRule::getInstId, instId);

        return baseMapper.selectList(wrapper);
    }

    /**
     * 保存历史记录
     *
     * @param cinstructionruleList
     * @throws RedpException
     */
    private void BizHisData(List<CInstructionRule> cinstructionruleListNew, List<CInstructionRule> cinstructionruleList, UpmpUserInfoVO user, String instId, String oprMenuId, String oprMenuName, String lastUpddt) throws RedpException {

        List entitys = new ArrayList<>();
        List entityOlds = new ArrayList<>();
        List bizOpr = new ArrayList<>();
        List realIds = new ArrayList<>();

        if (null != cinstructionruleList) {
            Iterator<CInstructionRule> itMsir = cinstructionruleList.iterator();
            while (itMsir.hasNext()) {
                CInstructionRule msto = (CInstructionRule) itMsir.next();
                int flag = 0;

                Iterator<CInstructionRule> itMsi = cinstructionruleListNew.iterator();
                while (itMsi.hasNext()) {
                    CInstructionRule mst = (CInstructionRule) itMsi.next();
                    if (msto.getId().equals(mst.getId())) {
                        flag = 1;
                        break;
                    }
                }
                if (flag == 0) { // 正式表的数据在临时表中已经不存在
                    /*listDel.add(msto);*/
                    entitys.add(new CInstructionRule());
                    entityOlds.add(msto);
                    bizOpr.add(BizOprEnum.OPR_CANCEL.getType());
                    realIds.add(msto.getId());
                }
            }
        }

        if (null != cinstructionruleListNew) {//分析出待新增的数据
            Iterator<CInstructionRule> itMsir = cinstructionruleListNew.iterator();
            while (itMsir.hasNext()) {
                CInstructionRule mst = (CInstructionRule) itMsir.next();
                int flag = 0;

                Iterator<CInstructionRule> itMso = cinstructionruleList.iterator();
                while (itMso.hasNext()) {
                    CInstructionRule msto = (CInstructionRule) itMso.next();
                    if (msto.getId().equals(mst.getId())) {
                        flag = 1;
                        BeanUtils.copyProperties(mst, msto);
                        break;
                    }
                }
                if (flag == 0) { // 临时表中不存在的数据
                    entitys.add(mst);
                    entityOlds.add(new CInstructionRule());
                    bizOpr.add(BizOprEnum.OPR_ADD.getType());
                    realIds.add(mst.getId());
                }
            }
        }

        for (int j = 0; j < cinstructionruleList.size(); j++) {
            for (int i = 0; i < cinstructionruleListNew.size(); i++) {
                if (cinstructionruleListNew.get(i).getId().equals(cinstructionruleList.get(j).getId())) {
                    entitys.add(cinstructionruleListNew.get(i));
                    entityOlds.add(cinstructionruleList.get(j));
                    bizOpr.add(BizOprEnum.OPR_UPDATE.getType());
                    realIds.add(cinstructionruleList.get(j).getId());
                }
            }
        }


        bizHisService.saveBizHisList(entitys, entityOlds, user, user.getInstId(), bizOpr, lastUpddt, oprMenuId, oprMenuName, realIds, null, null, null);
    }

    /**
     * 新增、修改、删除临时表明细记录
     *
     * @param tmpList
     * @param instId
     * @param ename
     * @param InstructionType
     * @throws RedpException
     */
    public void BizTmpData(List<BizTmp> tmpList, String instId, String ename, String InstructionType) throws RedpException {

        List<BizTmp> listOrg = getBizTmpListByUniqueKey(instId, ename, InstructionType);
        List<BizTmp> listDel = new ArrayList<BizTmp>();//待删除的数据
        List<BizTmp> listNew = new ArrayList<BizTmp>();//待新增的数据

        if (null != listOrg) {
            Iterator<BizTmp> itMsir = listOrg.iterator();
            while (itMsir.hasNext()) {
                BizTmp msto = (BizTmp) itMsir.next();
                int flag = 0;

                Iterator<BizTmp> itMsi = tmpList.iterator();
                while (itMsi.hasNext()) {
                    BizTmp mst = (BizTmp) itMsi.next();
                    if (msto.getRealId().equals(mst.getRealId())) {
                        flag = 1;
                        break;
                    }
                }
                if (flag == 0) { // 正式表的数据在临时表中已经不存在
                    listDel.add(msto);
                }
            }
        }

        if (null != tmpList) {//分析出待新增的数据
            Iterator<BizTmp> itMsir = tmpList.iterator();
            while (itMsir.hasNext()) {
                BizTmp mst = (BizTmp) itMsir.next();
                int flag = 0;

                Iterator<BizTmp> itMso = listOrg.iterator();
                while (itMso.hasNext()) {
                    BizTmp msto = (BizTmp) itMso.next();
                    if (msto.getRealId().equals(mst.getRealId())) {
                        flag = 1;
                        BeanUtils.copyProperties(mst, msto);
                        break;
                    }
                }
                if (flag == 0) { // 临时表中不存在的数据
                    listNew.add(mst);
                }
            }
        }
        if (Common.isNotBlank(listOrg) && listOrg.size() > 0) {//修改
            bizTmpService.saveOrUpdateBatch(listOrg);
        }
        if (Common.isNotBlank(listNew) && listNew.size() > 0) {//新增
            bizTmpService.saveOrUpdateBatch(listNew);
        }
        if (Common.isNotBlank(listDel) && listDel.size() > 0) {//删除
            bizTmpService.delete(listDel);
        }
    }


    private void upstatus(String instructionType, String type, String status, String authUid, String lastUpddt) {
        LambdaUpdateWrapper<CInstructionRule> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(CInstructionRule::getInstructionType, instructionType).set(CInstructionRule::getAuditStatus, type)
                .set(CInstructionRule::getOprStatus, status).set(CInstructionRule::getOprId, authUid)
                .set(CInstructionRule::getUpdateTime, lastUpddt);
        this.update(wrapper);

    }


}
